import React, { Component } from 'react'
import { Route ,Redirect,NavLink,Switch} from 'react-router-dom'
import pubsub from 'pubsub-js'
import './header.scss'
import './js/btn'

export default class header extends Component {
    constructor(props){
        super(props)
        this.state={
            search:"",
            page:"首页",
            bg:"url(../index_img.jpg)"
        }
        pubsub.subscribe("component",(msg,data)=>{
            if (data=="首页") {
                this.setState({page:data,bg:"url(../index_img.jpg)"})
            }else{
                this.setState({page:data,bg:"url(../tags_img.jpg)"})
            }
            document.body.scrollTop = 0;
            document.documentElement.scrollTop = 0;
        })
    }
    search=(e)=>{
        this.setState({search:this.refs.searchContent.value},()=>{
            pubsub.publish("search",this.refs.searchContent.value)
            this.refs.search.click()
        })
    }
    searchText=(e)=>{
        if (e.keyCode==13) {
            this.refs.searchBtn.click()
        }
    }
    render(){
    return (
        <>
            <header className={this.state.page=="首页"?"":"header"} style={{backgroundImage:this.state.bg}}>
                <div className="inner">
                    <nav>
                        <h1>个人博客</h1>
                        <ul>
                            <li>
                                <NavLink to='/index' title="首页">
                                    <svg style={{position: 'relative',top:'2px'}} width="16" height="16" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><rect width="48" height="48" fill="white" fill-opacity="0.01"/><path d="M9 18V42H39V18L24 6L9 18Z" fill="none"/><path d="M9 42V18L4 22L24 6L44 22L39 18V42H9Z" stroke="#333" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/><path d="M19 29V42H29V29H19Z" fill="none" stroke="#333" stroke-width="4" stroke-linejoin="round"/><path d="M9 42H39" stroke="#333" stroke-width="4" stroke-linecap="round"/></svg>
                                    <span>首页</span>
                                </NavLink>
                            </li>
                            <li>
                                <NavLink to='/tags' title="标签">
                                    <svg style={{position: "relative",top:"2px"}} width="16" height="16" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><rect width="48" height="48" fill="white" fill-opacity="0.01"/><path d="M42.1691 29.2451L29.2631 42.1511C28.5879 42.8271 27.6716 43.2069 26.7161 43.2069C25.7606 43.2069 24.8444 42.8271 24.1691 42.1511L8 26V8H26L42.1691 24.1691C43.5649 25.5732 43.5649 27.841 42.1691 29.2451Z" fill="none" stroke="#333" stroke-width="4" stroke-linejoin="round"/><path fill-rule="evenodd" clip-rule="evenodd" d="M18.5 21C19.8807 21 21 19.8807 21 18.5C21 17.1193 19.8807 16 18.5 16C17.1193 16 16 17.1193 16 18.5C16 19.8807 17.1193 21 18.5 21Z" fill="#333"/></svg>
                                    <span>标签</span>
                                </NavLink>
                            </li>
                            <li>
                                <NavLink to='/introduction' title="简介">
                                    <svg style={{position: "relative",top:"2px"}} width="16" height="16" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><circle cx="24" cy="12" r="8" fill="none" stroke="#333" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/><path d="M42 44C42 34.0589 33.9411 26 24 26C14.0589 26 6 34.0589 6 44" stroke="#333" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/></svg>
                                    <span>简介</span>
                                </NavLink>
                            </li>
                            <li>
                                <NavLink to='/message' title="留言">
                                    <svg style={{position: "relative",top:"2px"}} width="16" height="16" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><rect width="48" height="48" fill="white" fill-opacity="0.01"/><path d="M33 38H22V30H36V22H44V38H39L36 41L33 38Z" stroke="#333" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/><path d="M4 6H36V30H17L13 34L9 30H4V6Z" fill="none" stroke="#333" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/><path d="M19 18H20" stroke="#333" stroke-width="4" stroke-linecap="round"/><path d="M26 18H27" stroke="#333" stroke-width="4" stroke-linecap="round"/><path d="M12 18H13" stroke="#333" stroke-width="4" stroke-linecap="round"/></svg>
                                    <span>留言</span>
                                </NavLink>
                            </li>
                            <li>
                                <button ref="searchBtn" onClick={this.search}>
                                    <svg width="16" height="16" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><rect width="48" height="48" fill="white" fill-opacity="0.01"/><path d="M21 38C30.3888 38 38 30.3888 38 21C38 11.6112 30.3888 4 21 4C11.6112 4 4 11.6112 4 21C4 30.3888 11.6112 38 21 38Z" fill="none" stroke="#333" stroke-width="4" stroke-linejoin="round"/><path d="M26.6568 14.3431C25.2091 12.8954 23.2091 12 21 12C18.7909 12 16.7909 12.8954 15.3431 14.3431" stroke="#333" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/><path d="M33.2218 33.2218L41.7071 41.7071" stroke="#333" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/></svg>
                                </button>
                                <NavLink to={{pathname:"/search",state:{data:this.state.search,type:"搜索"}}} ref="search" >
                                </NavLink>
                                <input type="text" placeholder="  搜索" title="搜索" ref="searchContent" onKeyUp={this.searchText} />
                            </li>
                        </ul>
                    </nav>
                </div>
                <div className="header-title">
                    <h2>{this.state.page}</h2>
                </div>
                <div className="header-btn">
                    <i>&gt;</i>
                </div>
        </header>
        {/* 返回顶部或者返回底部 */}
        <button className="top-bottom">
            <svg className="goTop" width="30" height="30" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M24.0083 14.1005V42" stroke="#333" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/><path d="M12 26L24 14L36 26" stroke="#333" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/><path d="M12 6H36" stroke="#333" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/></svg>
            <svg className="goBottom" width="30" height="30" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><rect width="48" height="48" fill="white" fill-opacity="0.01"/><path d="M24.0083 33.8995V6" stroke="#333" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/><path d="M36 22L24 34L12 22" stroke="#333" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/><path d="M36 42H12" stroke="#333" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/></svg>
        </button>
        </>
    )}
}
